(function (vc) {
    vc.extends({
        propTypes: {
            callBackListener: vc.propTypes.string, //父组件名称
            callBackFunction: vc.propTypes.string //父组件监听方法
        },
        data: {
            addShopVipUserInfo: {
                vipId: '',
                vipName: '',
                vipTel: '',
                sex: '',
                vipLevel: '',
                birthday: '',
                startTime: '',
                endTime: '',
                vipLevels: [],
                sexs: []
            }
        },
        _initMethod: function () {
            vc.initDate('addBirthday', function (_value) {
                $that.addShopVipUserInfo.birthday = _value;
            });
            vc.initDate('addStartTime', function (_value) {
                $that.addShopVipUserInfo.startTime = _value;
            });
            vc.initDate('addEndTime', function (_value) {
                $that.addShopVipUserInfo.endTime = _value;
            });
            vc.getDict('s_shop_vip_user', "sex", function (_data) {
                $that.addShopVipUserInfo.sexs = _data;
            });
        },
        _initEvent: function () {
            vc.on('addShopVipUser', 'openAddShopVipUserModal', function () {
                $that._listAddShopVipLevels();
                $('#addShopVipUserModel').modal('show');
            });
        },
        methods: {
            addShopVipUserValidate() {
                return vc.validate.validate({
                    addShopVipUserInfo: $that.addShopVipUserInfo
                }, {
                    'addShopVipUserInfo.vipName': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员名称不能为空"
                        },
                        {
                            limit: "maxLength",
                            param: "64",
                            errInfo: "会员名称太长"
                        },
                    ],
                    'addShopVipUserInfo.vipTel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员电话不能为空"
                        },
                        {
                            limit: "phone",
                            param: "",
                            errInfo: "会员电话格式错误"
                        },
                    ],
                    'addShopVipUserInfo.sex': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "性别不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "性别错误"
                        },
                    ],
                    'addShopVipUserInfo.vipLevel': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员等级不能为空"
                        },
                        {
                            limit: "num",
                            param: "",
                            errInfo: "会员等级错误"
                        },
                    ],
                    'addShopVipUserInfo.birthday': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "会员生日不能为空"
                        },
                        {
                            limit: "date",
                            param: "",
                            errInfo: "会员生日格式错误"
                        },
                    ],
                    'addShopVipUserInfo.startTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "开始时间不能为空"
                        },
                        {
                            limit: "date",
                            param: "",
                            errInfo: "开始时间格式错误"
                        },
                    ],
                    'addShopVipUserInfo.endTime': [
                        {
                            limit: "required",
                            param: "",
                            errInfo: "结束时间不能为空"
                        },
                        {
                            limit: "date",
                            param: "",
                            errInfo: "结束时间格式错误"
                        },
                    ]
                });
            },
            saveShopVipUserInfo: function () {
                if (!$that.addShopVipUserValidate()) {
                    vc.toast(vc.validate.errInfo);
                    return;
                }
                $that.addShopVipUserInfo.shopId = vc.getCurrentCommunity().shopId;
                //不提交数据将数据 回调给侦听处理
                if (vc.notNull($props.callBackListener)) {
                    vc.emit($props.callBackListener, $props.callBackFunction, $that.addShopVipUserInfo);
                    $('#addShopVipUserModel').modal('hide');
                    return;
                }
                vc.http.apiPost(
                    '/shopVipUser/saveShopVipUser',
                    JSON.stringify($that.addShopVipUserInfo),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        if (_json.code == 0) {
                            //关闭model
                            $('#addShopVipUserModel').modal('hide');
                            $that.clearAddShopVipUserInfo();
                            vc.emit('shopVipUserManage', 'listShopVipUser', {});
                            vc.toast("添加成功");
                            return;
                        }
                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');
                        vc.toast(errInfo);
                    });
            },
            clearAddShopVipUserInfo: function () {
                $that.addShopVipUserInfo = {
                    vipName: '',
                    vipTel: '',
                    sex: '',
                    vipLevel: '',
                    birthday: '',
                    startTime: '',
                    endTime: '',
                    vipLevels: []
                };
            },
            _listAddShopVipLevels: function (_page, _rows) {
                var param = {
                    params: {
                        page: 1,
                        row: 50,
                        shopId: vc.getCurrentCommunity().shopId
                    }
                };
                //发送get请求
                vc.http.apiGet('/shopVipUser.queryShopVipLevel',
                    param,
                    function (json, res) {
                        var _shopVipLevelManageInfo = JSON.parse(json);
                        $that.addShopVipUserInfo.vipLevels = _shopVipLevelManageInfo.data;
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
        }
    });
})(window.vc);
